export default {
  namespaced: true,
  state: {
    orderSocket: null, // 订单推送
    chatSocket: null, // 聊天推送
    newOrders: [], // 订单推送过来的订单数组
    tabbarItemIndex: 0, //
    loadMoreTotal: 15, // 加载更多的数据量
    toast: { // 提示框
      show: false,
      text: '',
      position: 'top',
      time: 2000,
      type: 'text'
    },
    loading: { // 加载框
      show: false,
      text: ''
    },
    loadMore: { // 更多加载框
      showLoading: true,
      tip: '正在加载',
      backgroundColor: '#fbf9fe'
    },
    signature: { // 签名
      data: '', // 数据
      type: '' // 类型
    },
    appVersion: '0.0.1' // App版本
    // showUpdateDialog: showUpdateDialog
  },
  mutations: {
    tabbarItemIndexChange (state, index) {
      state.tabbarItemIndex = index
    }
  },
  actions: {
    initApp ({ dispatch }, { rtk, uid, un }) {
      dispatch('user/initUser', {
        rtk, uid, un
      }, { root: true }).then(res => {
        dispatch('order/initOrder', {}, { root: true })
        dispatch('chat/initChat', {}, { root: true })
      }, err => {
        dispatch('showToast', {
          text: err
        })
      })
    },
    showToast ({ state }, { text, position }) {
      Object.assign(state.toast, {
        show: true,
        text,
        position
      })
    },
    showLoading ({ state }, { text }) {
      Object.assign(state.loading, {
        show: true,
        text: text || '加载中'
      })
    },
    hideLoading ({ state }) {
      Object.assign(state.loading, {
        show: false
      })
    },
    loadMore ({ state }, { listLength }) {
      Object.assign(state.loadMore, {
        showLoading: listLength ? !(listLength < state.loadMoreTotal) : false,
        tip: listLength ? listLength < state.loadMoreTotal ? '已加载全部' : '正在加载' : '暂无数据'
      })
    }
  }
}
